<?php
namespace Controller;

use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;

class Main extends Base
{

    public function __construct(Container $container)
    {
        parent::__construct($container);




    }
    public function register(Request $req, Response $res,$args)
    {
        $weichat=$this->weichat;
        $tcp_token=$this->redis->get('tcp_token');
        if(!empty($tcp_token)&&isset($_SESSION['openid'])){
            require_once SYSTEM_PATH . "/app/lib/jssdk.php";
            $jssdk = new \JSSDK($weichat['appid'], $weichat['appsecret']);
            $signPackage = $jssdk->GetSignPackage();
            $this->assign = [
                'openid'=>$_SESSION['openid'],
                'signPackage'=>$signPackage,
            ];

        }else{
            // 第一步：用户同意授权，获取code
            $callbackurl=$_SERVER['REQUEST_URI'];

            //snsapi_userinfo  snsapi_base
            $codeurl=sprintf('https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=%s#wechat_redirect',$weichat['appid'],$weichat['redirect_uri'],$callbackurl);
            header('Location:'.$codeurl);
            exit;
        }
        $this->setToken();
        //邀请你的用户ID
        if(isset($args['uid'])&&!empty($args['uid'])){
            $this->assign['uid']=$args['uid'];
        }


        $this->view->assign($this->assign);
        $this->view->display('main/register.html');
    }
    public function edit(Request $req, Response $res)
    {

        $user=$this->getUser();
        $this->checkweichat($user);
        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{
           $children=$this->db->where(['uid'=>$user['uid']])->select('tcp_user_children');
           foreach ($children as $key=>$value){
               $children[$key]['year']=date('Y',$value['birthday']);
               $children[$key]['month']=date('n',$value['birthday']);
               $children[$key]['day']=date('j',$value['birthday']);
           }
            $this->setToken();
            $this->assign['baby']=$children;
            $this->assign['user']=$user;
            $this->view->assign($this->assign);
            $this->view->display('main/edit.html');
        }

    }
    public function index(Request $req, Response $res)
    {

        $user=$this->getUser();
        $this->checkweichat($user);



        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{

            $this->assign['user']=$user;
            $this->view->assign($this->assign);
            $this->view->display('main/index.html');
        }


    }
    public function  invitation(Request $req, Response $res){
        $user=$this->getUser();
        $this->checkweichat($user);

        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{
            $firstday = date('Y-m-01');//当月的第一天
            $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//当月的一天最后

            $where['uid']=array($user['uid']);
            $where['createtime']=array(strtotime($firstday),'>=');
            $where['createtime']=array(strtotime($lastday),'<=');
            $usercount=$this->db->where($where)->count('tcp_user_invitation');
            if($usercount>=30){
                $usercount=30;
            }
            $pess=round($usercount/30 * 100).'%' ;
            $this->setToken();
            $this->assign['user']=$user;
            $this->assign['count']=$usercount;
            $this->assign['pess']=$pess;
            $this->assign['num']=30-$usercount;
            $this->view->assign($this->assign);
            $this->view->display('main/invitation.html');
        }
    }
    public function sign(Request $req, Response $res)
    {

        $user=$this->getUser();
        $this->checkweichat($user);

        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{

            $this->setToken();
            $this->assign['time']=date('Y年m月d日');
            $this->assign['week']=Date("w", strtotime(Date("Y-n-1")));
            $this->assign['day']=date('d');
            $this->assign['totalday']=date("t");
            $this->assign['user']=$user;
            $this->view->assign($this->assign);
            $this->view->display('main/sign.html');
        }


    }
    public function luck(Request $req, Response $res)
    {

        $user=$this->getUser();
        $this->checkweichat($user);

        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{

            $this->setToken();
            $this->assign['user']=$user;
            $this->view->assign($this->assign);
            $this->view->display('main/luck.html');
        }


    }
    public function activity(Request $req, Response $res)
    {

        $user=$this->getUser();
        $this->checkweichat($user);
        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{


            $this->assign['user']=$user;
            $this->view->assign($this->assign);
            $this->view->display('main/activity.html');
        }


    }

    public function clothing(Request $req, Response $res)
    {

        $user=$this->getUser();
        $this->checkweichat($user);
        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{
            $this->assign['user']=$user;
            $this->view->assign($this->assign);
            $this->view->display('main/clothing.html');
        }

    }
    public function activity_detail(Request $req, Response $res, $args)
    {

        $user=$this->getUser();
        $this->checkweichat($user);
        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{
            if (!empty($args['id'])) {
                $result = $this->db->where(['id' => $args['id']])->find('tcp_activity');
                if (!empty($result)) {
                    $where['uid']=array((int)$user['uid']);
                    $where['act_id']=array($args['id']);
                    $result['issign']= $this->db->where($where)->count('tcp_user_activity');
                    unset($where['uid']);
                    $result['joinnum']= $this->db->where($where)->count('tcp_user_activity');
                    $this->assign = array_merge($this->assign,$result);


                } else {
                    return $res->withRedirect('/activity');
                    exit;
                }
            }
            $this->setToken();
            $this->assign['user']=$user;

            $this->view->assign($this->assign);
            $this->view->display('main/activity_detail.html');
        }

    }
    public function clothing_detail(Request $req, Response $res, $args)
    {

        $user=$this->getUser();
        $this->checkweichat($user);
        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{
            if (!empty($args['cid'])) {
                $result = $this->db->where(['id' => $args['cid']])->find('tcp_clothing');
                if (!empty($result)) {
                    $result['color'][] = $this->db->where(['cid' => $args['cid']])->find('tcp_clothing_color');
                    $result['size'][] = $this->db->where(['cid' => $args['cid']])->find('tcp_clothing_size');
                    $where['cid']=array($args['cid']);
                    $result['joinnum']= $this->db->where($where)->count('tcp_user_clothing');
                    $this->assign = array_merge($this->assign,$result);

                } else {
                    return $res->withRedirect('/clothing');
                    exit;
                }
            }
            $this->setToken();
            $this->assign['user']=$user;

            $this->view->assign($this->assign);
            $this->view->display('main/clothing_detail.html');
        }

    }

    public function share(Request $req, Response $res)
    {

        $user=$this->getUser();
        $this->checkweichat($user);
        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{
            $this->setToken();
            $this->assign['user']=$user;

            $this->view->assign($this->assign);
            $this->view->display('main/share.html');
         }



    }

    public function sharelist(Request $req, Response $res)
    {

        $user=$this->getUser();
        $this->checkweichat($user);
        if(empty($user)){
            return $res->withRedirect('/register');exit;
        }else{

            $this->assign['user']=$user;
            $this->view->assign($this->assign);
            $this->view->display('main/sharelist.html');
        }



    }

    public function event(Request $req, Response $res, $args)
    {
        //获得几个参数
        $token = 'atmediatest';//此处填写之前开发者配置的token
        $nonce = $_GET['nonce'];
        $timestamp = $_GET['timestamp'];
        $echostr = $_GET['echostr'];
        $signature = $_GET['signature'];
        //参数字典序排序
        $array = array($nonce, $timestamp, $token);
        sort($array);
        //验证
        $str = sha1(implode($array));//sha1加密
        //对比验证处理好的str与signature,若确认此次GET请求来自微信服务器，请原样返回echostr参数内容，则接入生效，成为开发者成功，否则接入失败。
        if ($str == $signature && $echostr) {

            //第一次接入微信api有echostr这个参数，之后就没有了
            echo $echostr;
        } else {
            //接入成功后的其他处理
            echo 1;

        }
    }

}